import axios from "axios";
import fs from "fs";

function getGeojson(code) {
  return axios.get('https://geo.datav.aliyun.com/areas_v3/bound/geojson', {
    params: {
      code
    }
  }).then(resp => {
    return resp.data
  }).catch(e=>{
    console.log(e.response.data)
  })
}

async function fetch(code) {
  let file = `./temp/${code}.json`
  return await getGeojson(code).then(content=>{
    if(!content){
      console.log(`${file} not`)
      return {}
    }
    fs.writeFileSync(file, JSON.stringify(content))
    return content
  })
}

async function adcodeFetch(adcode) {
  console.log(`fetching ${adcode}`)
  await fetch(adcode)

  let geojson,features
  if(String(adcode).endsWith('00')){
    geojson = await fetch(`${adcode}_full`)
    features = geojson?.features || []
    for (let i = 0; i < features.length; i++) {
      await adcodeFetch(features[i].properties.adcode)
    }
  }
  console.log(`fetched ${adcode}`)
}

// 启动
await adcodeFetch('100000')



